<?php
/**
 * cache_file.class.php 文件缓存操作类
 *
 * 继承于db.i.class.php接口
 *
 * @author          hyperblue
 * @version         0.1
 * @copyright       (C) 2011- *
 * @license	        http://www.bingceng.com
 * @lastmodify	    2011-11-14 10:30
 */
 
defined('BC_INC_PATH') or exit("Access Denied!");

class cache_file implements Icache {

	private $_config = array();

	/**
	 * 构造函数
	 */
	public function __construct($config = null) {
		$this->_config = $config;
	}
	
	/**
	 * 写入缓存
	 * @param	string	$name		缓存名称
	 * @param	mixed	$data		缓存数据
	 * @param	string	$type		缓存类型
	 * @param	string	$module		所属模型
	 * @return  mixed				缓存路径/false
	 */
	public function set($name, $data, $type = 'data', $module = ROUTE_M) {
		global $_config;
		if(empty($module)) $module = ROUTE_M;
		
		$cache_path = BC_CACHE_PATH.DS.$module.DS;
	    if(!is_dir($cache_path)) mkdir($cache_path, 0777, true);
		
		$cache_name = $name.$this->_config['suffix'];
		$cache_file = $cache_path.$cache_name;
	
		if($type == 'array') {
	    	$data = "<?php\nreturn ".var_export($data, true).";\n?>";
	    } elseif($type == 'serialize') {
	    	$data = serialize($data);
	    }
		
		//是否开启互斥锁
		if($_config['lock_ex']) {
			$cache_size = file_put_contents($cache_file, $data, LOCK_EX);
		} else {
			$cache_size = file_put_contents($cache_file, $data);
		}
		
		@chmod($cache_file, 0777);
		
	    return $cache_size ? $cache_size : 'false';
	}
	
	/**
	 * 获取缓存
	 * @param	string	$name		缓存名称
	 * @param	string	$type		缓存类型
	 * @param	string	$module		所属模型
	 * @return  mixed	$data		缓存数据
	 */
	public function get($name, $type = 'data', $module = ROUTE_M) {
		if(empty($module)) $module = ROUTE_M;
		
		$cache_path = BC_CACHE_PATH.DS.$module.DS;
		$cache_name = $name.$this->_config['suffix'];
		$cache_file = $cache_path.$cache_name;

		if (!file_exists($cache_file)) {
			return false;
		} else {
		    if($type == 'array') {
		    	$data = @require($cache_file);		
		    } elseif($type == 'serialize') {
		    	$data = unserialize(file_get_contents($cache_file));
		    } else {
				$data = file_get_contents($cache_file);
			}
		    
		    return $data;
		}
	}
	
	/**
	 * 删除缓存
	 * @param	string	$name		缓存名称
	 * @param	string	$type		缓存类型
	 * @param	string	$module		所属模型
	 * @return  bool
	 */
	public function delete($name, $type = 'data', $module = ROUTE_M) {
		if(empty($module)) $module = ROUTE_M;	
		
		$cache_path = BC_CACHE_PATH.DS.$module.DS;
		$cache_name = $name.$this->_config['suffix'];
		$cache_file = $cache_path.$cache_name;
		
		if(file_exists($cache_file)) {
			return @unlink($cache_file) ? true : false;
		} else {
			return false;
		}
	}
	
	/**
	 * 验证缓存是否存在或过期
	 */
	public function verify($name, $module = ROUTE_M) {
		global $_config;
		if(empty($module)) $module = ROUTE_M;
		
		$cache_path = BC_CACHE_PATH.DS.$module.DS;
		$cache_name = $name.$this->_config['suffix'];
		$cache_file = $cache_path.$cache_name;
		
		$cache_refresh = $_config['cache_refresh'];
		
		if(!file_exists($cache_file) || filemtime($cache_file) < (time() - $cache_refresh)) {
			return false;
		}
		
		return true;
		
	}
	
	/**
	 * 获取缓存名称
	 */
	public function path($name, $module = ROUTE_M) {
		$cache_path = BC_CACHE_PATH.DS.$module.DS;
		$cache_name = $name.$this->_config['suffix'];
		$cache_file = $cache_path.$cache_name;
		
		return $cache_file;
	}

}

?>
